package userHelp

import (
	"common/helper"
	myredis "common/redis"
	"fmt"
	"strconv"
)

type TblSwitchData struct {
	Deposit  float64 `json:"deposit" db:"deposit" cbor:"deposit"`
	Withdraw float64 `json:"withdraw" db:"withdraw" cbor:"withdraw"`
}

func InsertSql(logKey string, SqlContent string) {
	helper.InfoLog("%s-%s", logKey, SqlContent)
	_, err := g_sql.Exec(SqlContent)
	if err != nil {
		helper.SqlErrLog("%s:%s-------%s", logKey, SqlContent, err.Error())
	}
}

func UpdateSql(logKey string, SqlContent string) int64 {
	helper.InfoLog("%s---%s", logKey, SqlContent)
	reuslt, err := g_sql.Exec(SqlContent)
	if err != nil {
		helper.SqlErrLog("%s:%s-------%s", logKey, SqlContent, err.Error())
		return 0
	}

	count, err := reuslt.RowsAffected()
	if err != nil {
		helper.SqlErrLog("res.RowsAffected() returned error: %s", err.Error())
		return 0
	}

	return count
}

/*
切换上级 即换绑
newParentId 新绑定的上级 如为0 即 uid只做解绑操作
*/

func SwitchParent(uid string, newParentId string) {
	HandleTblDeposit(uid, newParentId)
	HandleProxyAccu(uid, newParentId)
	HandleReportProxyDaily(uid, newParentId)
	HandleReportDaily(uid, newParentId)
	HandleOnlineUser(uid, newParentId)
	HandleProxyBase(uid, newParentId)
}

func HandleProxyBase(uid string, newParentId string) {

	helper.InfoLog("HandleProxyBase begin")
	newLv1 := newParentId
	newLv2 := GetProxyParentId(newParentId)
	newLv3 := GetProxyGrandId(newParentId)

	newParentName := GetMemberName(newParentId)
	newGrandName := GetProxyParentName(newParentId)
	newGreatGrandName := GetProxyGrandName(newParentId)

	//先处理自己
	sqLUpdate := fmt.Sprintf("update tbl_proxy_base set "+
		"parent_id=%s,"+
		"grand_id=%s,"+
		"great_grand_id=%s,"+
		"parent_name='%s', "+
		"grand_name='%s', "+
		"great_grand_name='%s' "+
		"where uid=%s",
		newLv1, newLv2, newLv3, newParentName, newGrandName, newGreatGrandName, uid)

	UpdateSql("HandleProxyBase:0", sqLUpdate)
	//再处理下一级
	sqLUpdate = fmt.Sprintf("update tbl_proxy_base set "+
		"grand_id=%s,"+
		"great_grand_id=%s,"+
		"grand_name='%s', "+
		"great_grand_name='%s' "+
		"where parent_id=%s",
		newLv1, newLv2, newParentName, newGrandName, uid)
	UpdateSql("HandleProxyBase:1", sqLUpdate)

	//再处理下二级
	sqLUpdate = fmt.Sprintf("update tbl_proxy_base set "+
		"great_grand_id=%s,"+
		"great_grand_name='%s' "+
		"where grand_id=%s",
		newLv1, newParentName, uid)
	UpdateSql("HandleProxyBase:2", sqLUpdate)

	//处理渠道ID 跟业务员ID newParentId=0仅解绑 不处理处理渠道ID 跟业务员ID
	if newParentId != "0" {
		newBusiId := GetMemberBusinessId(newParentId)
		newOptId := GetMemberOperatorId(newParentId)

		//先处理自己
		sqLUpdate = fmt.Sprintf("update tbl_member_base set operator_id=%s,business_id=%s "+
			"where uid = %s", newBusiId, newOptId, uid)
		UpdateSql("tbl_member_base:0", sqLUpdate)

		sqLUpdate = fmt.Sprintf("update tbl_member_base set operator_id=%s,business_id=%s "+
			"where uid in (SELECT uid from tbl_proxy_base where parent_id=%s or grand_id=%s or great_grand_id=%s)", newBusiId, newOptId, newLv1, newLv1, newLv1)
		UpdateSql("tbl_member_base:other", sqLUpdate)

		sqLUpdate = fmt.Sprintf("update tbl_member_base set operator_id=%s,business_id=%s "+
			"where uid in (SELECT uid from tbl_proxy_base where parent_id=%s or grand_id=%s or great_grand_id=%s)", newBusiId, newOptId, uid, uid, uid)
		UpdateSql("tbl_member_base:other", sqLUpdate)
	}

	helper.InfoLog("HandleProxyBase end")
}
func HandleTblDeposit(uid string, newParentId string) {
	//处理tbl_deposit表中的parent_id
	helper.InfoLog("HandleTblDeposit begin")
	sqLUpdate := fmt.Sprintf("update tbl_deposit set parent_id=%s where uid=%s", newParentId, uid)
	UpdateSql("HandleTblDeposit[tbl_deposit]", sqLUpdate)

	//处理tbl_withdraw表中的parent_id
	sqLUpdate = fmt.Sprintf("update tbl_withdraw set parent_id=%s where uid=%s", newParentId, uid)
	UpdateSql("HandleTblDeposit[tbl_withdraw]", sqLUpdate)

	helper.InfoLog("HandleTblDeposit begin")
}
func HandleProxyAccu(uid string, newParentId string) {

	helper.InfoLog("HandleProxyAccu begin")
	OldLv1 := GetProxyParentId(uid)
	OldLv2 := GetProxyGrandId(uid)
	OldLv3 := GetProxyGreatGrandId(uid)

	newLv1 := newParentId
	newLv2 := GetProxyParentId(newParentId)
	newLv3 := GetProxyGrandId(newParentId)

	// 处理tbl_proxy_accu 表中的数据 仅充值与提现数据
	var selfData TblSwitchData
	SqlSelect := fmt.Sprintf("select deposit,withdraw from tbl_member_accu where uid=%s", uid)
	helper.InfoLog(SqlSelect)
	g_sql.Get(&selfData, SqlSelect)

	var DataProxylv1 TblSwitchData
	SqlSelect = fmt.Sprintf("select deposit,withdraw from tbl_proxy_accu where uid=%s and level=1", uid)
	helper.InfoLog(SqlSelect)
	g_sql.Get(&DataProxylv1, SqlSelect)

	var DataProxylv2 TblSwitchData
	SqlSelect = fmt.Sprintf("select deposit,withdraw from tbl_proxy_accu where uid=%s and level=2", uid)
	helper.InfoLog(SqlSelect)
	g_sql.Get(&DataProxylv2, SqlSelect)

	var sqLUpdate string = ""
	//先减掉原先上级的数据
	if OldLv1 != "0" {
		sqLUpdate = fmt.Sprintf("update tbl_proxy_accu set deposit=deposit-%0.2f,withdraw=withdraw-%0.2f where uid=%s and level=1",
			selfData.Deposit, selfData.Withdraw, OldLv1)

		UpdateSql("HandleTblDeposit[tbl_proxy_accu:OldLv1]", sqLUpdate)

		sqLUpdate = fmt.Sprintf("update tbl_proxy_accu set deposit=deposit-%0.2f,withdraw=withdraw-%0.2f where uid=%s and level=2",
			DataProxylv1.Deposit, DataProxylv1.Withdraw, OldLv1)
		UpdateSql("HandleTblDeposit[tbl_proxy_accu:OldLv1]", sqLUpdate)

		sqLUpdate = fmt.Sprintf("update tbl_proxy_accu set deposit=deposit-%0.2f,withdraw=withdraw-%0.2f where uid=%s and level=3",
			DataProxylv2.Deposit, DataProxylv2.Withdraw, OldLv1)
		UpdateSql("HandleTblDeposit[tbl_proxy_accu:OldLv1]", sqLUpdate)
	}

	if OldLv2 != "0" {
		sqLUpdate = fmt.Sprintf("update tbl_proxy_accu set deposit=deposit-%0.2f,withdraw=withdraw-%0.2f where uid=%s and level=2",
			selfData.Deposit, selfData.Withdraw, OldLv2)
		UpdateSql("HandleTblDeposit[tbl_proxy_accu:OldLv2]", sqLUpdate)

		sqLUpdate = fmt.Sprintf("update tbl_proxy_accu set deposit=deposit-%0.2f,withdraw=withdraw-%0.2f where uid=%s and level=3",
			DataProxylv1.Deposit, DataProxylv1.Withdraw, OldLv2)
		UpdateSql("HandleTblDeposit[tbl_proxy_accu:OldLv2]", sqLUpdate)
	}

	if OldLv3 != "0" {
		sqLUpdate = fmt.Sprintf("update tbl_proxy_accu set deposit=deposit-%0.2f,withdraw=withdraw-%0.2f where uid=%s and level=3",
			selfData.Deposit, selfData.Withdraw, OldLv3)
		UpdateSql("HandleTblDeposit[tbl_proxy_accu:OldLv3]", sqLUpdate)
	}

	//往新上级加数据
	if newLv1 != "0" {
		sqLUpdate = fmt.Sprintf("update tbl_proxy_accu set deposit=deposit+%0.2f,withdraw=withdraw+%0.2f where uid=%s and level=1",
			selfData.Deposit, selfData.Withdraw, newLv1)
		UpdateSql("HandleTblDeposit[tbl_proxy_accu:newLv1]", sqLUpdate)
		sqLUpdate = fmt.Sprintf("update tbl_proxy_accu set deposit=deposit+%0.2f,withdraw=withdraw+%0.2f where uid=%s and level=2",
			DataProxylv1.Deposit, DataProxylv1.Withdraw, newLv1)
		UpdateSql("HandleTblDeposit[tbl_proxy_accu:OldLv3]", sqLUpdate)
		sqLUpdate = fmt.Sprintf("update tbl_proxy_accu set deposit=deposit+%0.2f,withdraw=withdraw+%0.2f where uid=%s and level=3",
			DataProxylv2.Deposit, DataProxylv2.Withdraw, newLv1)
		UpdateSql("HandleTblDeposit[tbl_proxy_accu:OldLv3]", sqLUpdate)
	}

	if newLv2 != "0" {
		sqLUpdate = fmt.Sprintf("update tbl_proxy_accu set deposit=deposit+%0.2f,withdraw=withdraw+%0.2f where uid=%s and level=2",
			selfData.Deposit, selfData.Withdraw, newLv2)
		UpdateSql("HandleTblDeposit[tbl_proxy_accu:newLv2]", sqLUpdate)
		sqLUpdate = fmt.Sprintf("update tbl_proxy_accu set deposit=deposit+%0.2f,withdraw=withdraw+%0.2f where uid=%s and level=3",
			DataProxylv1.Deposit, DataProxylv1.Withdraw, newLv2)
		UpdateSql("HandleTblDeposit[tbl_proxy_accu:newLv2]", sqLUpdate)
	}

	if newLv3 != "0" {
		sqLUpdate = fmt.Sprintf("update tbl_proxy_accu set deposit=deposit+%0.2f,withdraw=withdraw+%0.2f where uid=%s and level=3",
			selfData.Deposit, selfData.Withdraw, newLv3)
		UpdateSql("HandleTblDeposit[tbl_proxy_accu:newLv3]", sqLUpdate)
	}

	helper.InfoLog("HandleProxyAccu end")
}

func IsSameBusi(uid string, newParentId string) bool {
	if newParentId == "0" {
		return true
	}

	OldBusiId := GetMemberBusinessId(uid)
	newBusiId := GetMemberBusinessId(newParentId)

	if OldBusiId == newBusiId {
		return true
	} else {
		return false
	}
}

func IsSameOpt(uid string, newParentId string) bool {
	if newParentId == "0" {
		return true
	}

	OldOpt := GetMemberOperatorId(uid)
	NewOpt := GetMemberOperatorId(newParentId)

	if OldOpt == NewOpt {
		return true
	} else {
		return false
	}
}

// 处理在线的相关用户
func HandleOnlineUser(uid string, newParentId string) {

	helper.InfoLog("HandleOnlineUser begin")
	newLv1 := newParentId
	newLv2 := GetProxyParentId(newParentId)
	newLv3 := GetProxyGrandId(newParentId)

	newParentName := GetMemberName(newParentId)
	newGrandName := GetProxyParentName(newParentId)
	newGreatGrandName := GetProxyGrandName(newParentId)

	var bSameBusi = IsSameBusi(uid, newParentId)
	newBusiId := GetMemberBusinessId(newParentId)
	newOpt := GetMemberOperatorId(newParentId)
	if myredis.IsUserExis(uid) {
		SetProxyParentId(uid, newLv1)
		SetProxyGrandId(uid, newLv2)
		SetProxyGreatGrandId(uid, newLv3)

		SetProxyParentName(uid, newParentName)
		SetProxyGrandName(uid, newGrandName)
		SetProxyGreatGrandName(uid, newGreatGrandName)

		if !bSameBusi {
			SetMemberBusinessId(uid, newBusiId)
			SetMemberOperatorId(uid, newOpt)
		}
	}

	var uidlv1 []int
	SqlSelect := fmt.Sprintf("SELECT uid from tbl_proxy_base where parent_id=%s", uid)
	helper.InfoLog(SqlSelect)
	g_sql.Select(&uidlv1, SqlSelect)

	for _, childId := range uidlv1 {
		strchildId := strconv.Itoa(childId)
		if myredis.IsUserExis(strchildId) {
			SetProxyGrandId(strchildId, newLv1)
			SetProxyGreatGrandId(strchildId, newLv2)

			SetProxyGrandName(strchildId, newParentName)
			SetProxyGreatGrandName(strchildId, newGrandName)
			if !bSameBusi {
				SetMemberBusinessId(uid, newBusiId)
				SetMemberOperatorId(uid, newOpt)
			}
		}
	}

	var uidlv2 []int
	SqlSelect = fmt.Sprintf("SELECT uid from tbl_proxy_base where grand_id=%s", uid)
	helper.InfoLog(SqlSelect)
	g_sql.Select(&uidlv2, SqlSelect)

	for _, childId := range uidlv2 {
		strchildId := strconv.Itoa(childId)
		if myredis.IsUserExis(strchildId) {
			SetProxyGreatGrandId(strchildId, newLv1)
			SetProxyGreatGrandName(strchildId, newParentName)
			if !bSameBusi {
				SetMemberBusinessId(uid, newBusiId)
				SetMemberOperatorId(uid, newOpt)
			}
		}
	}

	//只处理busi opt
	var uidlv3 []int
	SqlSelect = fmt.Sprintf("SELECT uid from tbl_proxy_base where great_grand_id=%s", uid)
	helper.InfoLog(SqlSelect)
	g_sql.Select(&uidlv3, SqlSelect)

	for _, childId := range uidlv3 {
		strchildId := strconv.Itoa(childId)
		if myredis.IsUserExis(strchildId) && !bSameBusi {
			SetMemberBusinessId(uid, newBusiId)
			SetMemberOperatorId(uid, newOpt)
		}
	}

	helper.InfoLog("HandleOnlineUser end")
}

func UpdateReportUserDaily(uid string, level int, singleDate string, DataDaily TblSwitchData) {
	if uid == "0" {
		return
	}

	if DataDaily.Deposit == 0.0 && DataDaily.Withdraw == 0.0 {
		return
	}

	sqLUpdate := fmt.Sprintf("update tbl_report_proxy_daily set deposit=deposit-%0.2f,withdraw=withdraw-%0.2f where uid=%s and level=%d and created_at='%s'",
		DataDaily.Deposit, DataDaily.Withdraw, uid, level, singleDate)
	rowEffect := UpdateSql("UpdateReportUserDaily[tbl_report_proxy_daily]", sqLUpdate)
	if rowEffect == 0 {
		SqlInsert := fmt.Sprintf("Insert into tbl_report_proxy_daily(uid,level,created_at,deposit,withdraw) values (%s,%d,'%s',%0.2f,%0.2f)",
			uid, level, singleDate, DataDaily.Deposit, DataDaily.Withdraw)
		InsertSql("UpdateReportUserDaily[tbl_report_proxy_daily]", SqlInsert)
	}
}

func UpdateReportNewUserDaily(uid string, level int, singleDate string, DataDaily TblSwitchData) {

	if uid == "0" {
		return
	}

	if DataDaily.Deposit == 0.0 && DataDaily.Withdraw == 0.0 {
		return
	}

	sqLUpdate := fmt.Sprintf("update tbl_report_proxy_daily set deposit=deposit+%0.2f,withdraw=withdraw+%0.2f where uid=%s and level=%d and created_at='%s'",
		DataDaily.Deposit, DataDaily.Withdraw, uid, level, singleDate)
	rowEffect := UpdateSql("UpdateReportNewUserDaily[tbl_report_proxy_daily]", sqLUpdate)
	if rowEffect == 0 {
		SqlInsert := fmt.Sprintf("Insert into tbl_report_proxy_daily(uid,level,created_at,deposit,withdraw) values (%s,%d,'%s',%0.2f,%0.2f)",
			uid, level, singleDate, DataDaily.Deposit, DataDaily.Withdraw)
		InsertSql("UpdateReportNewUserDaily[tbl_report_proxy_daily]", SqlInsert)
	}
}

// 处理 tbl_report_proxy_daily 表  代理日表数据  不一定每天都会有数据 update 不成功 需要insert
func HandleReportProxyDaily(uid string, newParentId string) {
	//先查询日期

	helper.InfoLog("HandleReportProxyDaily begin")
	var allDate []string
	SqlSelect := fmt.Sprintf("SELECT  DISTINCT(created_at) from tbl_report_daily ORDER BY created_at;")
	helper.InfoLog(SqlSelect)
	g_sql.Select(&allDate, SqlSelect)
	OldLv1 := GetProxyParentId(uid)
	OldLv2 := GetProxyGrandId(uid)
	OldLv3 := GetProxyGreatGrandId(uid)

	newLv1 := newParentId
	newLv2 := GetProxyParentId(newParentId)
	newLv3 := GetProxyGrandId(newParentId)

	for _, singleDate := range allDate {
		//	helper.InfoLog("date:%s", singleDate)
		subSingleDate := singleDate[:10]
		var selfData TblSwitchData
		SqlSelect := fmt.Sprintf("select deposit,withdraw from tbl_report_user_daily where uid=%s and created_at='%s'", uid, subSingleDate)
		helper.InfoLog(SqlSelect)
		g_sql.Get(&selfData, SqlSelect)

		var DataProxylv1 TblSwitchData
		SqlSelect = fmt.Sprintf("select deposit,withdraw from tbl_report_proxy_daily where uid=%s and level=1 and created_at='%s'", uid, subSingleDate)
		helper.InfoLog(SqlSelect)
		g_sql.Get(&DataProxylv1, SqlSelect)

		var DataProxylv2 TblSwitchData
		SqlSelect = fmt.Sprintf("select deposit,withdraw from tbl_report_proxy_daily where uid=%s and level=2 and created_at='%s'", uid, subSingleDate)
		helper.InfoLog(SqlSelect)
		g_sql.Get(&DataProxylv2, SqlSelect)

		UpdateReportUserDaily(OldLv1, 1, subSingleDate, selfData)
		UpdateReportUserDaily(OldLv1, 2, subSingleDate, DataProxylv1)
		UpdateReportUserDaily(OldLv1, 3, subSingleDate, DataProxylv2)

		UpdateReportUserDaily(OldLv2, 2, subSingleDate, selfData)
		UpdateReportUserDaily(OldLv2, 3, subSingleDate, DataProxylv1)

		UpdateReportUserDaily(OldLv3, 3, subSingleDate, selfData)

		//往新上级加数据
		UpdateReportNewUserDaily(newLv1, 1, subSingleDate, selfData)
		UpdateReportNewUserDaily(newLv1, 2, subSingleDate, DataProxylv1)
		UpdateReportNewUserDaily(newLv1, 3, subSingleDate, DataProxylv2)

		UpdateReportNewUserDaily(newLv2, 2, subSingleDate, selfData)
		UpdateReportNewUserDaily(newLv2, 3, subSingleDate, DataProxylv1)

		UpdateReportNewUserDaily(newLv3, 3, subSingleDate, selfData)
	}

	helper.InfoLog("HandleReportProxyDaily begin")
}

// 处理 tbl_report_daily 表
func HandleReportDaily(uid string, newParentId string) {

	//newParentId 为0 则只是做解绑操作 意味着业务员跟渠道的数据不需要变化
	if IsSameBusi(uid, newParentId) {
		return
	}

	helper.InfoLog("HandleReportDaily begin")
	//先查询日期
	var allDate []string
	SqlSelect := fmt.Sprintf("SELECT DISTINCT(created_at) from tbl_report_daily ORDER BY created_at;")
	helper.InfoLog(SqlSelect)
	g_sql.Select(&allDate, SqlSelect)

	//处理渠道ID 跟业务员ID
	newBusiId := GetMemberBusinessId(newParentId)
	newOptId := GetMemberOperatorId(newParentId)

	OldBusiId := GetMemberBusinessId(uid)
	OldOptId := GetMemberOperatorId(uid)

	for _, singleDate := range allDate {
		subSingleDate := singleDate[:10]
		var selfData TblSwitchData
		SqlSelect := fmt.Sprintf("select deposit,withdraw from tbl_report_user_daily where uid=%s and created_at='%s'", uid, subSingleDate)
		helper.InfoLog(SqlSelect)
		g_sql.Get(&selfData, SqlSelect)

		var DataProxy TblSwitchData
		SqlSelect = fmt.Sprintf("select sum(deposit) as deposit,sum(withdraw) as withdraw from tbl_report_proxy_daily where uid=%s and created_at='%s'", uid, subSingleDate)
		helper.InfoLog(SqlSelect)
		g_sql.Get(&DataProxy, SqlSelect)

		//先减掉原先业务员的数据
		var totalDeposit float64 = 0.0
		totalDeposit = selfData.Deposit + DataProxy.Deposit

		var totalWithdraw float64 = 0.0
		totalWithdraw = selfData.Withdraw + DataProxy.Withdraw

		if totalDeposit == 0.0 && totalWithdraw == 0.0 {
			continue
		}

		sqLUpdate := fmt.Sprintf("update tbl_report_daily set deposit=deposit-%0.2f,withdraw=withdraw-%0.2f "+
			"where business_id=%s and operator_id=%s and created_at='%s'",
			totalDeposit, totalWithdraw, OldBusiId, OldOptId, subSingleDate)
		rowEffect := UpdateSql("HandleReportDaily[tbl_report_daily]", sqLUpdate)
		if rowEffect == 0 {
			SqlInsert := fmt.Sprintf("Insert into tbl_report_daily(business_id,operator_id,created_at,deposit,withdraw) values (%s,%s,'%s',%0.2f,%0.2f)",
				OldBusiId, OldOptId, subSingleDate, totalDeposit, totalWithdraw)
			InsertSql("HandleReportDaily[tbl_report_daily]", SqlInsert)
		}

		// 防止 business_id=0 operator_id=888888的数据被减二次
		if !IsSameOpt(uid, newParentId) {
			//处理当天Opt对应的汇总
			sqLUpdate := fmt.Sprintf("update tbl_report_daily set deposit=deposit-%0.2f,withdraw=withdraw-%0.2f "+
				"where business_id=0 and operator_id=%s and created_at='%s'", totalDeposit, totalWithdraw, OldOptId, subSingleDate)

			rowEffect := UpdateSql("HandleReportDaily[tbl_report_daily]", sqLUpdate)
			if rowEffect == 0 {
				SqlInsert := fmt.Sprintf("Insert into tbl_report_daily(business_id,operator_id,created_at,deposit,withdraw) values ('%s',%0.2f,%0.2f)",
					0, OldOptId, subSingleDate, totalDeposit, totalWithdraw)
				InsertSql("HandleReportDaily[tbl_report_daily]", SqlInsert)
			}
		}

		//数据加到新的业务员
		sqLUpdate = fmt.Sprintf("update tbl_report_daily set deposit=deposit+%0.2f,withdraw=withdraw+%0.2f "+
			"where business_id=%s and operator_id=%s and created_at='%s'",
			totalDeposit, totalWithdraw, newBusiId, newOptId, subSingleDate)
		rowEffect = UpdateSql("HandleReportDaily[tbl_report_daily]", sqLUpdate)
		if rowEffect == 0 {
			SqlInsert := fmt.Sprintf("Insert into tbl_report_daily(business_id,operator_id,created_at,deposit,withdraw) values (%s,%s,'%s',%0.2f,%0.2f)",
				newBusiId, newOptId, subSingleDate, totalDeposit, totalWithdraw)
			InsertSql("HandleReportDaily[tbl_report_daily]", SqlInsert)
		}

		if !IsSameOpt(uid, newParentId) {
			//处理当天Opt对应的汇总
			sqLUpdate := fmt.Sprintf("update tbl_report_daily set deposit=deposit+%0.2f,withdraw=withdraw+%0.2f "+
				"where business_id=0 and operator_id=%s and created_at='%s'", totalDeposit, totalWithdraw, newOptId, subSingleDate)
			rowEffect := UpdateSql("HandleReportDaily[tbl_report_daily]", sqLUpdate)

			if rowEffect == 0 {
				SqlInsert := fmt.Sprintf("Insert into tbl_report_daily(business_id,operator_id,created_at,deposit,withdraw) values ('%s',%0.2f,%0.2f)",
					newBusiId, subSingleDate, totalDeposit, totalWithdraw)
				InsertSql("HandleReportDaily[tbl_report_daily]", SqlInsert)
			}
		}
	}

	helper.InfoLog("HandleReportDaily end")
}
